var OrderStatTable = {
    id: "oderStatTable", //表格id
    seItem: null,		//选中的条目
    me: "",
    href: "",
    countIndex: ""
};

/**
 * 初始化整个表格
 */
OrderStatTable.init = function (current) {
    var queryData = {};
    //结束时间
    var oneDayAfter = new Date().DateAdd('d', 1);
    var val_search_endDate = $("#txt_search_endDate").val();
    if (val_search_endDate !== "" && val_search_endDate !== null) {
        queryData['endDate'] = val_search_endDate;
    } else {
        $("#txt_search_endDate").val(formatDate(oneDayAfter, "yyyy-MM-dd"));
        queryData['endDate'] = formatDate(oneDayAfter, "yyyy-MM-dd");
    }
    //开始时间
    var val_search_startDate = $("#txt_search_startDate").val();
    if (val_search_startDate !== "" && val_search_startDate !== null) {
        queryData['startDate'] = val_search_startDate;
    } else {
        $("#txt_search_startDate").val(formatDate(new Date(), "yyyy-MM-dd"));
        queryData['startDate'] = formatDate(new Date(), "yyyy-MM-dd");
    }

    var defaultColunms = this.initColumn();
    var bstable = new BSTable(OrderStatTable.id, Feng.ctxPath + "/orderStat/allParam", defaultColunms);
    bstable.setData(queryData);
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
OrderStatTable.initColumn = function () {
    var columns = [
        {
            title: '序号', align: 'center', valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index + 1;
            }
        },
        {title: '学员姓名', field: 'userName', align: 'center', valign: 'middle'},
        {title: '报班单号', field: 'orderNum', align: 'center', valign: 'middle'},
        {title: '缴费方式', field: 'orderPayment', align: 'center', valign: 'middle'},
        {title: '课程名称', field: 'courseName', align: 'center', valign: 'middle'},
        {title: '课程课次', field: 'courseTotalNumber', align: 'center', valign: 'middle'},
        {title: '课程校区', field: 'courseCampus', align: 'center', valign: 'middle'},
        {title: '主讲老师', field: 'courseTeacherMain', align: 'center', valign: 'middle'},
        {title: '学管师', field: 'courseTeacherHelp', align: 'center', valign: 'middle'},
        {
            title: '报班课次', field: 'purchaseTimes', align: 'center', valign: 'middle', formatter: function (value, row, index) {
            return '+' + value;
        }
        },
        {
            title: '报班金额', field: 'purchaseAmount', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return numeral(value).format('0,0.00');
            }
        },
        {
            title: '优惠券优惠金额', field: 'courseDiscountAmount', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return numeral(value).format('0,0.00');
            }
        },
        {
            title: '转入课次', field: 'inToTimes', align: 'center', valign: 'middle', formatter: function (value, row, index) {
            return '+' + value;
        }
        },
        {
            title: '转入金额', field: 'inToAmount', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return '+' + numeral(value).format('0,0.00');
            }
        },
        {
            title: '转出课次', field: 'turnOutTimes', align: 'center', valign: 'middle', formatter: function (value, row, index) {
            return '-' + value;
        }
        },
        {
            title: '转出金额', field: 'turnOutAmount', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return '-' + numeral(value).format('0,0.00');
            }
        },
        {
            title: '退费课次', field: 'refundTimes', align: 'center', valign: 'middle', formatter: function (value, row, index) {
            return '-' + value;
        }
        },
        {
            title: '退费金额', field: 'refundAmount', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return '-' + numeral(value).format('0,0.00');
            }
        },
        {
            title: '课时合计', field: 'totalTimes', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value >= 0) {
                    return '+' + numeral(value).format('0,0.00');
                } else {
                    return numeral(value).format('0,0.00');
                }
            }
        },
        {
            title: '业绩合计', field: 'totalAmount', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value >= 0) {
                    return '+' + numeral(value).format('0,0.00');
                } else {
                    return numeral(value).format('0,0.00');
                }
            }
        },
        {title: '操作时间', field: 'createTime', align: 'center', valign: 'middle'}
    ];
    return columns;
};

/**
 * 点击搜索按钮
 */
function clickSearch() {
    var queryData = {};
    var option = getFormJson("#formSearch");
    //订单号
    var val_search_orderNum = $("#txt_search_orderNum").val();
    if (val_search_orderNum !== "" && val_search_orderNum !== null) {
        queryData['orderNum'] = val_search_orderNum;
        option.orderNum = val_search_orderNum;
    }
    //学员名称
    var val_search_studentName = $("#txt_search_studentName").val();
    if (val_search_studentName !== "" && val_search_studentName !== null) {
        queryData['userName'] = val_search_studentName;
        option.userName = val_search_studentName;
    }
    //课程校区
    var val_search_campus = $("#txt_search_campus").val();
    if (val_search_campus !== "" && val_search_campus !== null) {
        queryData['courseCampus'] = val_search_campus;
        option.courseCampus = val_search_campus;
    }
    //开始时间
    var val_search_startDate = $("#txt_search_startDate").val();
    if (val_search_startDate !== "" && val_search_startDate !== null) {
        queryData['startDate'] = val_search_startDate;
        option.startDate = val_search_startDate;
    } else {
        Feng.info("请选择开始时间");
        return;
    }
    //结束时间
    var val_search_endDate = $("#txt_search_endDate").val();
    if (val_search_endDate !== "" && val_search_endDate !== null) {
        queryData['endDate'] = val_search_endDate;
        option.endDate = val_search_endDate;
    } else {
        Feng.info("请选择结束时间");
        return;
    }
    OrderStatTable.me.destroy(queryData);

    //统计合计金额
    var ajax = new $ax(Feng.ctxPath + "/orderStat/stat", function (data) {
        if (data.success && null !== data.obj) {
            $("#value_stat_total_times").text(data.obj.statTotalTimes);
            $("#value_stat_total_amount").text(data.obj.statTotalAmount);
        }
    }, function (data) {
        Feng.error("统计失败!");
    });
    ajax.setData(option);
    ajax.start();

}

/**
 * 清空搜索栏
 */
function resetSearchVal() {
    //订单号
    $("#txt_search_orderNum").val("");
    //学生姓名
    $("#txt_search_studentName").val("");
    //课程校区
    $("#txt_search_campus").val("");
    //开始时间
    $("#txt_search_startDate").val("");
    //结束时间
    $("#txt_search_endDate").val("");
}

/**
 * 报表信息导出
 */
$("#btn_oderStat_export").on("click",function () {
    //订单号
    var val_search_orderNum = $("#txt_search_orderNum").val();
    //学员名称
    var val_search_studentName = $("#txt_search_studentName").val();
    var val_search_campus = $("#txt_search_campus").val();
    var val_search_startDate = $("#txt_search_startDate").val();
    if (val_search_startDate === null || val_search_startDate.length <= 0) {
        Feng.info("请选择开始时间");
        return;
    }
    //结束时间
    var val_search_endDate = $("#txt_search_endDate").val();
    if (val_search_endDate === null || val_search_endDate.length <= 0) {
        Feng.info("请选择结束时间");
        return;
    }
    var url = Feng.ctxPath + "/orderStat/export?orderNum="+val_search_orderNum+"&name="+val_search_studentName
        + "&campus=" + val_search_campus + "&startDate=" + val_search_startDate + "&endDate=" + val_search_endDate;
    window.location = url;
});

$(function () {
    OrderStatTable.init(1);
});
